package com.data.service.manage.impl;

import com.data.domain.manage.Orgauth;
import com.data.domain.manage.OrgauthCriteria;
import com.data.domain.manage.Orgauth;
import com.data.domain.manage.OrgauthCriteria;
import com.data.mapper.manage.OrgauthMapper;
import com.data.model.ManagerModel;
import com.data.service.manage.OrgauthService;
import com.data.utill.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "orgauthService")
public class OrgauthServiceImpl implements OrgauthService {

    @Autowired
    private OrgauthMapper orgauthMapper;

    @Override
    public List<Orgauth> getOrgauth(ManagerModel managerModel){
        OrgauthCriteria orgauthCriteria=new OrgauthCriteria();
        OrgauthCriteria.Criteria criteria=orgauthCriteria.createCriteria();
        if(StringUtils.isNoNullString(StringUtils.getIdFromString(managerModel.getSender()))){
            criteria.andSenderEqualTo(StringUtils.getIdFromString(managerModel.getSender()));
        }
        if(StringUtils.isNoNullString(StringUtils.getIdFromString(managerModel.getRecver()))){
            criteria.andRecverEqualTo(StringUtils.getIdFromString(managerModel.getRecver()));
        }
        orgauthCriteria.setOrderByClause("id asc");
        return orgauthMapper.selectByExample(orgauthCriteria);
    }

    /**
     * 入网机构信息插入
     * @param managerModel
     * @return
     */
    @Override
    public int insertOrgauth(ManagerModel managerModel) {
        OrgauthCriteria orgauthCriteria=new OrgauthCriteria();
        Orgauth data = new Orgauth();
//        data.setId(Integer.parseInt(managerModel.getId()));
        data.setSender(managerModel.getSender());
        data.setRecver(managerModel.getRecver());
        data.setForbid(managerModel.getForbid());
        return orgauthMapper.insertSelective(data);
    }

    /**
     * 入网机构信息修改
     * @param managerModel
     * @return
     */
    @Override
    public int modifyOrgauth(ManagerModel managerModel ,int id) {
        OrgauthCriteria orgauthCriteria=new OrgauthCriteria();
//        orgauthCriteria.createCriteria().andIdEqualTo(id);
        Orgauth data = new Orgauth();
        data.setId(Integer.parseInt(managerModel.getId()));
        data.setSender(managerModel.getSender());
        data.setRecver(managerModel.getRecver());
        data.setForbid(managerModel.getForbid());
        return orgauthMapper.updateByExampleSelective(data,orgauthCriteria);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Orgauth getOrgauthById(int id) {
        OrgauthCriteria orgauthCriteria=new OrgauthCriteria();
        orgauthCriteria.createCriteria().andIdEqualTo(id);
        List<Orgauth> data = orgauthMapper.selectByExample(orgauthCriteria);
        if (null!=data) {
            return data.get(0);
        }else{
            return null;
        }
    }
}
